﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;
using Dapper;
using Newtonsoft.Json;
using API_Service.DBHelper;
using System.Configuration;

namespace API_Service.BizLogic.Bom
{
    public class OutputPlanService : BaseLogicService
    {
        public OutputPlanService() { }

        public ExecutionResult<dynamic> GetOutputData()
        {
            var result = new ExecutionResult<dynamic>();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    string sql = " select * from c_output_plan_t where create_date > GETDATE() - 5 ";

                    conn.Open();

                    result.Datas = conn.Query<dynamic>(sql).ToArray();
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);

                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }

        public ExecutionResult<dynamic> SaveOutput(string viewModel)
        {
            var result = new ExecutionResult<dynamic>();

            dynamic vm = JsonConvert.DeserializeObject<dynamic>(viewModel);

            using (IDbConnection conn = SqlCreator.Create())
            {
                conn.Open();

                IDbTransaction transaction = conn.BeginTransaction();

                try
                {
                    string sql_insert = " insert into c_output_plan_t values(@line,@pdate,@start_time,@end_time,@targetqty, @act_qty,@createdate,@createuser) ";

                    string sql_delete = " delete c_output_plan_t where line_name=@line and production_date=@pdate ";

                    result.ExecuteCount = conn.Execute(sql_delete, new { line = (string)vm.line, pdate = (string)vm.pdate }, transaction);

                    var p = new List<dynamic>();

                    foreach (var v in vm.values)
                    {
                        p.Add(new
                        {
                            line = (string)v.LineName,
                            pdate = (string)v.Production_Date,
                            start_time = (string)v.StartTime,
                            end_time = (string)v.EndTime,
                            targetqty = (int)v.TargetQty,
                            act_qty = 0,
                            createdate = (DateTime)v.CreateDate,
                            createuser = (string)v.CreateUser
                        });
                    }

                    result.ExecuteCount = conn.Execute(sql_insert, p, transaction);
                    result.Message = "产能数据维护完成.";

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();

                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);

                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
    }
}